package cn.bocweb.spinning.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumDelBean;
import cn.bocweb.spinning.model.Bean.ForumMyCollectBean;
import cn.bocweb.spinning.model.Bean.UserBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.adapter.CollectTopicAdapter;
import cn.bocweb.spinning.view.common.SwipeRefreshHelper;
import cn.bocweb.spinning.view.forun.ForunDetailActivity;
import de.greenrobot.event.EventBus;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class CollectTopicFragment extends Fragment {
    @Bind(R.id.listView)

    ListView listView;
    CollectTopicAdapter adapter;
    List<ForumMyCollectBean.DataBean> list;
    int page = 1;
    String user;
    UserPresenter mPresenter;
    List<Integer> aidList;
    @Bind(R.id.refresh)
    SwipeRefreshLayout mRefresh;

    SwipeRefreshHelper mSwipeRefreshHelper;
    @Bind(R.id.my_collect_delete_all)
    CheckBox mMyCollectDeleteAll;
    @Bind(R.id.my_collect_delete_btn)
    Button mMyCollectDeleteBtn;
    @Bind(R.id.my_collect_delete_all_layout)
    RelativeLayout mMyCollectDeleteAllLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_collect_topic, container, false);
        EventBus.getDefault().register(this);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mPresenter = new UserPresenterImp();
        aidList = new ArrayList<>();
        loadData();

        mMyCollectDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StringBuffer buffer = new StringBuffer();

                for (int i = 0; i < list.size(); i++) {
                    if (adapter.getMap().containsKey(i)) {
                        if (adapter.map.get(i)) {
                            buffer.append(list.get(i).getId() + ",");
                            aidList.add(i);
                        }
                    }
                }
                buffer.delete(buffer.length() - 1, buffer.length());
                DeviceUtil.Log(buffer.toString());
                if (mMyCollectDeleteAll.isChecked()) {
                    mPresenter.forumDel(user, "1", "");
                } else {
                    mPresenter.forumDel(user, "2", buffer.toString());
                }
            }
        });
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    private void loadData() {
        list = new ArrayList<>();
        adapter = new CollectTopicAdapter(getActivity(), list, mMyCollectDeleteAll);
        listView.setAdapter(adapter);

        UserBean.DataEntity userBean = new UserBean.DataEntity();
        userBean.readInfoFromLocal(getActivity());
        user = userBean.getUserid();

        mPresenter.forumMyCollect(page, "9", user);

        mSwipeRefreshHelper = new SwipeRefreshHelper(mRefresh, listView, new SwipeRefreshHelper.OnSwipeRefreshListener() {
            @Override
            public void onRefresh() {
                list.clear();
                adapter.notifyDataSetChanged();
                page = 1;
                mPresenter.forumMyCollect(page, "9", user);
            }

            @Override
            public void onLoad() {
                page++;
                mPresenter.forumMyCollect(page, "9", user);
            }
        });

        mMyCollectDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), ForunDetailActivity.class);
                intent.putExtra(Constant.TO_FORUM_DETAIL, list.get(position).getId());
                startActivity(intent);
            }
        });
    }

    public void onEventMainThread(ForumMyCollectBean bean) {
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        }
        if (bean.getReturn_code() == 0) {
            list.addAll(bean.getData());
            adapter.notifyDataSetChanged();
        }
    }

    public void onEventMainThread(ForumDelBean bean) {
        if (bean.getReturn_code() == 0) {
            DeviceUtil.MyToast(getContext(), "删除成功");
            page = 1;
            list.clear();
            adapter.notifyDataSetChanged();
            mPresenter.forumMyCollect(page, "9", user);

        } else {
            DeviceUtil.Log(bean.getMsg());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public void clearAdapter() {
        adapter.setShowSelect(false);
        adapter.setSetAllSelect(false);
        adapter.notifyDataSetChanged();
        mMyCollectDeleteAll.setChecked(false);
        mMyCollectDeleteAllLayout.setVisibility(View.GONE);
    }

    public void deleteAdapter() {
        adapter.setShowSelect(true);
        adapter.notifyDataSetChanged();

        mMyCollectDeleteAllLayout.setVisibility(View.VISIBLE);
        mMyCollectDeleteAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    adapter.setSetAllSelect(true);
                    adapter.notifyDataSetChanged();
                } else if (adapter.isSetAllSelect()) {
                    adapter.setSetAllSelect(false);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }
}
